home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 565 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.8 KB  |  91 lines

  1. Path: chronicle.mti.sgi.com!austern
  2. From: Etay_Bogner@mail.stil.scitex.com (Etay Bogner)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Calling X(int) from X()'s init list
  5. Date: 27 Feb 1996 10:41:37 PST
  6. Organization: Scitex Corp.
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <Etay_Bogner-2702961023450001@metay.stil.scitex.com>
  9. References: <4gnush$phg@news.kth.se> <4gognf$et@news.bridge.net> <4gsa2r$gjm@sdaw04.seinf.abb.se>
  10. NNTP-Posting-Host: isolde.mti.sgi.com
  11. X-Original-Date: Tue, 27 Feb 1996 10:23:45 +0200
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBVAwUBMTNQd0y4NqrwXLNJAQGtkwH+JQv2cVwFX6UIh4pdSQXNKAcVaorvhYzc
  14.     nh9rXZ8V8oVO1jgKKbnz8hGxyI97NJ+C+MUR+5izcQTJPNI/BfGW5w==
  15.     =EJ7w
  16. Originator: austern@isolde.mti.sgi.com
  17.  
  18. In article <4gsa2r$gjm@sdaw04.seinf.abb.se>, alindbac@sw.seisy.abb.se wrote:
  19.  
  20. << In article <4gognf$et@news.bridge.net>,
  21. << David Byrden  <100101.2547@compuserve.com> wrote:
  22. << ><<<<<<<
  23. << >
  24. << >Why is it not allowed to delegate the initialisation to another
  25. << >constructor
  26. << >
  27. << >class X
  28. << >{
  29. << >public:
  30. << >    X (int i) : i_(i) {}
  31. << >
  32. << >    X () : X(34) {}             // Not allowed
  33. << >>>>>>>>>>
  34. << >
  35. << >
  36. << >     Why not use      X (int i = 34 ) : i_(i) {}
  37. << 
  38. << >[ I think he was concerned with the more general case of a complicated
  39. << >  initialization (not assignment) that must be repeated for each
  40. << >  constructor. -sdc, moderator
  41. << >]
  42. << 
  43. << Well, one could use a similar thing using a member function:
  44. << 
  45. << X (int i) { init(i); }
  46. << X () { init(34); }
  47. << 
  48. << where init is a member function for class X.
  49. << 
  50.  
  51. Not very useful for const or reference data members ( here, useful ==
  52. can't be done without explicitly initializing them, but than you "init"
  53. will do only half the work )
  54.  
  55. My inclination, although I rarly use const or reference data members, will
  56. be to wrap those data members in an internal structure with it's own
  57. constructor :
  58.  
  59. class A_Class_With_A_Const_Or_Refs {
  60.     A_Class_With_A_Const_Or_Refs(long& l);
  61.     struct internal_Data {
  62.         long& mLong;
  63.         const long mClassID; // when there was no RTTI, for instance
  64.         internal_Data(long& inLong, const long inID);
  65.         } mInternalData;
  66.     };
  67.  
  68. A_Class_With_A_Const_Or_Refs::A_Class_With_A_Const_Or_Refs(long& l) :
  69. mInternalData(l, 'MYID') {}
  70.  
  71. and so on.
  72.  
  73. In my opinion, those cases will be very rare.
  74.  
  75. -- 
  76. -- Etay Bogner
  77. -- Etay_Bogner@mail.stil.scitex.com
  78. -- Scitex Corp.
  79. -- Israel.
  80. -- 
  81. -- There are two rules for success in life:                   
  82. -- Rule 1:  Don't tell people everything you know.                
  83. -- 
  84. ---
  85. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  86.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  87.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  88.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  89.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  90. ]
  91.